﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TraCuu_Update
{
    public partial class NhapSachGUI : UserControl, GUI.UctrlGUI
    {
        class cbxItem
        {
            public int _ID;
            public String _Text;

            public cbxItem(int id, String text)
            {
                _ID = id;
                _Text = text;
            }

            public override string ToString()
            {
                return _Text;
            }
        }

        BLL.DanhSachPhieuNhapSachBLL _DanhSachPhieuNhapBLL;
        BLL.DanhSachSachBLL _DanhSachSachBLL;

        public NhapSachGUI()
        {
            InitializeComponent();
            _DanhSachPhieuNhapBLL = new BLL.DanhSachPhieuNhapSachBLL();
            _DanhSachSachBLL = new BLL.DanhSachSachBLL();
        }

        public String getName()
        {
            return "TRA CỨU PHIẾU NHẬP SÁCH";
        }

        private void NhapSachGUI_Load(object sender, EventArgs e)
        {
            UpdateCB();
            if (cbx_MaPhieu.Items.Count > 0)
                cbx_MaPhieu.SelectedIndex = 0;
            else
            {
                gv_List.Rows.Clear();
            }
        }

        private void UpdateCB()
        {
            cbx_MaPhieu.Items.Clear();
            List<DTO.PhieuNhapSachDTO> lis =
                _DanhSachPhieuNhapBLL.GetPhieuByNgayNhap(dt_NgayNhap.Value);
            foreach (DTO.PhieuNhapSachDTO dto in lis)
            {
                cbx_MaPhieu.Items.Add(new cbxItem(dto.ID, "Phiếu " + dto.ID.ToString()));
            }
        }

        private void UpdateGV()
        {
            gv_List.Rows.Clear();
            List<DTO.ChiTietNhapSachDTO> lis =
                _DanhSachPhieuNhapBLL.GetChiTietByPhieuNhap(
                (cbx_MaPhieu.SelectedItem as cbxItem)._ID);

            foreach (DTO.ChiTietNhapSachDTO dto in lis)
            {
                DTO.DauSachDTO s = _DanhSachSachBLL.GetSachByID(dto.IDSach);
                String[] item = new String[] { 
                    dto.IDSach.ToString(),
                    (gv_List.RowCount+1).ToString(),
                    s.Ten,
                    s.TheLoai,
                    s.TacGia,
                    dto.SoLuong.ToString()
                };
                gv_List.Rows.Add(item);
            }
        }

        private void cbx_MaPhieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateGV();
        }

        private void dt_NgayNhap_ValueChanged(object sender, EventArgs e)
        {
            UpdateCB();
            if (cbx_MaPhieu.Items.Count > 0)
                cbx_MaPhieu.SelectedIndex = 0;
            else
            {
                gv_List.Rows.Clear();
            }
        }
    }
}
